package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.en.HouseStatus;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @Author:hx
 * @DateTime:2022/9/27 13:39
 * @Description:
 */
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {
    @DubboReference
    private HouseService houseService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private DictService dictService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseUserService houseUserService;

    @RequestMapping
    public String index(Model model,@RequestParam Map<String,Object> filters){
        //条件
        saveAllDictToRequestScope(model);
        //分页
        PageInfo<House> page = houseService.findByPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "house/index";
    }

    @RequestMapping("/create")
    public String create(Model model){
        saveAllDictToRequestScope(model);
        return "house/create";
    }

    @RequestMapping("/save")
    public String insert(Model model,House house){
        house.setStatus(HouseStatus.UNPUBLISHED.code);
        houseService.insert(house);
        return successPage(model,"添加房源信息成功!");
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model,@PathVariable Long id){
        saveAllDictToRequestScope(model);
        House house = houseService.getById(id);
        model.addAttribute("house",house);
        return "house/edit";
    }

    @RequestMapping("/update")
    public String update(Model model,House house){
        houseService.update(house);
        return successPage(model,"修改房源信息成功!");
    }

    @RequestMapping("/delete/{id}")
    public String delete(Model model,@PathVariable Long id){
        houseService.delete(id);
        return "redirect:/house";
    }

    @RequestMapping("/publish/{id}/{status}")
    public String publish(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        houseService.publish(id,status);
        return "redirect:/house";
    }

    @RequestMapping("/{id}")
    public String show(Model model,@PathVariable Long id){
        //房源
        House house = houseService.getById(id);
        model.addAttribute("house",house);
        //小区
        Community community = communityService.getById(house.getCommunityId());
        model.addAttribute("community",community);
        //房源图片
        List<HouseImage> houseImage1List = houseImageService.findHouseImageListByHouseIdAndType(id,1);
        model.addAttribute("houseImage1List",houseImage1List);
        //房产图片
        List<HouseImage> houseImage2List = houseImageService.findHouseImageListByHouseIdAndType(id,2);
        model.addAttribute("houseImage2List",houseImage2List);
        //经纪人
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(id);
        model.addAttribute("houseBrokerList",houseBrokerList);
        //房东
        List<HouseUser> houseUserList = houseUserService.findHouseUserListByHouseId(id);
        model.addAttribute("houseUserList",houseUserList);

        return "house/show";
    }

    public void saveAllDictToRequestScope(Model model){
        model.addAttribute("communityList",communityService.findAll());
        model.addAttribute("houseTypeList",dictService.findDictListByParentDictCode("houseType"));
        model.addAttribute("floorList",dictService.findDictListByParentDictCode("floor"));
        model.addAttribute("buildStructureList",dictService.findDictListByParentDictCode("buildStructure"));
        model.addAttribute("directionList",dictService.findDictListByParentDictCode("direction"));
        model.addAttribute("decorationList",dictService.findDictListByParentDictCode("decoration"));
        model.addAttribute("houseUseList",dictService.findDictListByParentDictCode("houseUse"));
    }
}
